#include <iostream>
#include "TcpServer.hpp"
#include "Protocol.hpp"
#include "NetCal.hpp"
#include "Daemon.hpp"
#include "Log.hpp"

using namespace LogModule;

void Usage(const std::string &cmd)
{
    std::cerr << "Usage: " << cmd << " port" << std::endl;
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(USAGE_ERR);
    }
    uint16_t port = std::stoi(argv[1]);

    LOG(LogLevel::INFO) << "服务器已启动，已经是守护进程了！";
    Daemon(0, 0);

    std::cout << "你不应该看到我！" << std::endl;

    ENABLE_FILE_LOG_STRATEGY();

    //1.顶层
    std::unique_ptr<Cal> cal = std::unique_ptr<Cal>();

    //2.协议层
    std::unique_ptr<Protocol> protocol = std::make_unique<Protocol>([&cal](Request &req){
        return cal->Calculate(req);
    });

    //3.服务层
    std::unique_ptr<TcpServer> usvr = std::make_unique<TcpServer>(port, 
        [&protocol](std::shared_ptr<Socket> &psock, InetAddr &addr){
            protocol->GetRequest(psock, addr);
    });
    usvr->Run();

    return 0;
}